package com.michael.auth.biz.impl;

import com.michael.auth.biz.AuthBiz;
import com.michael.auth.jwt.JwtTokenProvider;
import com.michael.auth.request.LoginRequest;
import com.michael.auth.response.LoginResponse;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
public class AuthBizImpl implements AuthBiz {



    private final AuthenticationManager authenticationManager;
    private final JwtTokenProvider jwtTokenProvider;


    public AuthBizImpl(JwtTokenProvider jwtTokenProvider, AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
        this.jwtTokenProvider = jwtTokenProvider;
    }


    @Override
    public LoginResponse login(LoginRequest loginReq) {
        // 查询用户密码是否存在




        Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
                loginReq.getAccountId(), loginReq.getAccountId()));
        SecurityContextHolder.getContext().setAuthentication(authentication);
        String token = jwtTokenProvider.generateToken(authentication);

        return new LoginResponse();
    }
}
